###########################
### HOW DO EUROPEAN CITIZENS FORM THEIR VIEWS OF THE EU PUBLIC ADMINISTRATION?
### EXPLORING THE ROLE OF HEURISTICS
### JOURNAL OF POLITICAL INSTITUTIONS AND POLITICAL ECONOMY
### REPLICATION FILE OF THE EMPIRICAL ANALYSIS
### JAN P. VOGLER
###########################

###########################
### INSTALL REQUIRED PACKAGES
###########################

install.packages("ggplot2")
install.packages("lmtest")
install.packages("sandwich")



###########################
### READ DATA
###########################

EU_publicadmin_data=read.csv("EU_publicadmin_data.csv")



###########################
### TABLES IN THE ARTICLE
###########################

###########################
### RELATIONSHIP OF TRUST AND PERCEPTIONS OF CORRUPTION IN THE EU BUREAUCRACY
### TABLE 1 IN THE ARTICLE
###########################

lm_trust_corr=lm(trust_EUpa ~ corr_EUpa, data=EU_publicadmin_data)
summary(lm_trust_corr)



###########################
### OBTAIN ABSOLUTE VALUE OF CORRELATION (MENTIONED IN TEXT)
### (|COR| < 0.1)
###########################

abs(cor(EU_publicadmin_data$corr_EUpa,EU_publicadmin_data$trust_EUpa, use="complete.obs"))



###########################
### DESCRIPTIVE SUMMARY STATISTICS: EMPIRICAL ANALYSIS
### TABLE 2 IN THE ARTICLE
###########################

summary(EU_publicadmin_data)



###########################
### TRUST IN THE EU BUREAUCRACY
### TABLE 3 IN THE ARTICLE
###########################

lm_trust_01=lm(trust_EUpa ~ trust_centpa, data=EU_publicadmin_data)
summary(lm_trust_01)

lm_trust_02=lm(trust_EUpa ~ trust_locpa, data=EU_publicadmin_data)
summary(lm_trust_02)

lm_trust_03=lm(trust_EUpa ~ trust_centpa + trust_locpa, data=EU_publicadmin_data)
summary(lm_trust_03)

lm_trust_04=lm(trust_EUpa ~ trust_centpa + trust_locpa + direc_econ + EU_econeff, data=EU_publicadmin_data)
summary(lm_trust_04)

lm_trust_05=lm(trust_EUpa ~ trust_centpa + trust_locpa + direc_econ + EU_econeff + trust_police + trust_courts, data=EU_publicadmin_data)
summary(lm_trust_05)

lm_trust_06=lm(trust_EUpa ~ trust_centpa + trust_locpa + direc_econ + EU_econeff + trust_police + trust_courts + AU, data=EU_publicadmin_data)
summary(lm_trust_06)

lm_trust_full=lm(trust_EUpa ~ trust_centpa + trust_locpa + direc_econ + EU_econeff + trust_police + trust_courts + AU + factor(type) + age + work_pa + educ + income + female + capital, data=EU_publicadmin_data)
summary(lm_trust_full)



###########################
### PERCEPTIONS OF CORRUPTION IN THE EU BUREAUCRACY
### TABLE 4 IN THE ARTICLE
###########################

lm_corruption_01=lm(corr_EUpa ~ corr_centpa, data=EU_publicadmin_data)
summary(lm_corruption_01)

lm_corruption_02=lm(corr_EUpa ~ corruption, data=EU_publicadmin_data)
summary(lm_corruption_02)

lm_corruption_03=lm(corr_EUpa ~ corr_centpa + corruption, data=EU_publicadmin_data)
summary(lm_corruption_03)

lm_corruption_04=lm(corr_EUpa ~ corr_centpa + corruption + direc_econ + EU_econeff, data=EU_publicadmin_data)
summary(lm_corruption_04)

lm_corruption_05=lm(corr_EUpa ~ corr_centpa + corruption + direc_econ + EU_econeff + trust_police + trust_courts, data=EU_publicadmin_data)
summary(lm_corruption_05)

lm_corruption_06=lm(corr_EUpa ~ corr_centpa + corruption + direc_econ + EU_econeff + trust_police + trust_courts + AU, data=EU_publicadmin_data)
summary(lm_corruption_06)

lm_corruption_full=lm(corr_EUpa ~ corr_centpa + corruption + direc_econ + EU_econeff + trust_police + trust_courts + AU + factor(type) + age + work_pa + educ + income + female + capital, data=EU_publicadmin_data)
summary(lm_corruption_full)



###########################
### FIGURES IN THE ARTICLE
###########################

###########################
### CREATE NEW DATA FRAMES
### TO CREATE PREDICTED VALUE PLOTS
###########################

###########################
### VARIATION IN TRUST IN THE BUREAUCRACY
###########################

nd1 = data.frame(trust_centpa=seq(-3,3), trust_locpa=rep(mean(EU_publicadmin_data$trust_locpa, na.rm=T),7))
nd1

nd2 = data.frame(trust_centpa=rep(mean(EU_publicadmin_data$trust_centpa, na.rm=T),7), trust_locpa=seq(-3,3))
nd2

nd2_controls = data.frame(trust_centpa=rep(mean(EU_publicadmin_data$trust_centpa, na.rm=T),7), trust_locpa=seq(-3,3), direc_econ=rep(mean(EU_publicadmin_data$direc_econ, na.rm=T)), EU_econeff=rep(mean(EU_publicadmin_data$EU_econeff, na.rm=T)), trust_police=rep(mean(EU_publicadmin_data$trust_police, na.rm=T)), trust_courts=rep(mean(EU_publicadmin_data$trust_courts, na.rm=T)), AU=rep(0,7))
nd2_controls



###########################
### VARIATION IN PERCEIVED CORRUPTION IN THE BUREAUCRACY
###########################

nd3 = data.frame(corr_centpa=seq(-3,3), corruption=rep(mean(EU_publicadmin_data$corruption, na.rm=T),7))
nd3

nd4 = data.frame(corr_centpa=rep(mean(EU_publicadmin_data$corr_centpa, na.rm=T),7), corruption=seq(-3,3))
nd4

nd4_controls = data.frame(corr_centpa=rep(mean(EU_publicadmin_data$corr_centpa, na.rm=T),7), corruption=seq(-3,3), direc_econ=rep(mean(EU_publicadmin_data$direc_econ, na.rm=T)), EU_econeff=rep(mean(EU_publicadmin_data$EU_econeff, na.rm=T)), trust_police=rep(mean(EU_publicadmin_data$trust_police, na.rm=T)), trust_courts=rep(mean(EU_publicadmin_data$trust_courts, na.rm=T)), AU=rep(0,7))
nd4_controls



###########################
### TRUST IN THE EU BUREAUCRACY AND IN THE DOMESTIC CENTRAL BUREAUCRACY
### FIGURE 2 IN THE ARTICLE
###########################

### IDENTIFY PREDICTED VALUES

pred.p1 = predict(lm_trust_03, type="response", se.fit=TRUE, newdata=nd1)
pred.p1

gch.table = cbind(pred.p1$fit, pred.p1$se.fit)
gch.table

### CREATE THE PLOT

fit.p1 = pred.p1
fit.p1 = pred.p1$fit
low.p1 = pred.p1$fit - 1.645*pred.p1$se.fit
high.p1 = pred.p1$fit + 1.645*pred.p1$se.fit
cis.p1 = cbind(fit.p1, low.p1, high.p1)

cis.p1

pred.p1.gg=as.data.frame(pred.p1)
cis.p1.gg=as.data.frame(cis.p1)

pred.p1.gg

library(ggplot2)
ggplot(pred.p1.gg, aes(x=seq(-3,3,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p1.gg$low.p1, ymax=cis.p1.gg$high.p1), alpha=0.1, fill="Blue", color="Blue", linetype = 2) + scale_fill_brewer(palette="Blues") + xlab("Trust in Domestic Central Bureaucracy") + ylab("Trust in EU Bureaucracy") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5))



###########################
### TRUST IN THE EU BUREAUCRACY AND IN THE DOMESTIC LOCAL BUREAUCRACY (WITHOUT CONTROLS)
### FIGURE 3 (PANEL A) IN THE ARTICLE
###########################

### IDENTIFY PREDICTED VALUES

pred.p2 = predict(lm_trust_03, type="response", se.fit=TRUE, newdata=nd2)
pred.p2

gch.table = cbind(pred.p2$fit, pred.p2$se.fit)
gch.table

### CREATE THE PLOT

fit.p2 = pred.p2
fit.p2 = pred.p2$fit
low.p2 = pred.p2$fit - 1.645*pred.p2$se.fit
high.p2 = pred.p2$fit + 1.645*pred.p2$se.fit
cis.p2 = cbind(fit.p2, low.p2, high.p2)

cis.p2

pred.p2.gg=as.data.frame(pred.p2)
cis.p2.gg=as.data.frame(cis.p2)

library(ggplot2)
ggplot(pred.p2.gg, aes(x=seq(-3,3,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p2.gg$low.p2, ymax=cis.p2.gg$high.p2), alpha=0.1, fill="Blue", color="Blue", linetype = 2) + scale_fill_brewer(palette="Blues") + xlab("Trust in Domestic Local Bureaucracy") + ylab("Trust in EU Bureaucracy") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5))



###########################
### TRUST IN THE EU BUREAUCRACY AND IN THE DOMESTIC LOCAL BUREAUCRACY (WITH CONTROLS)
### FIGURE 3 (PANEL B) IN THE ARTICLE
###########################

### IDENTIFY PREDICTED VALUES

pred.p2 = predict(lm_trust_06, type="response", se.fit=TRUE, newdata=nd2_controls)
pred.p2

gch.table = cbind(pred.p2$fit, pred.p2$se.fit)
gch.table

### CREATE THE PLOT

fit.p2 = pred.p2
fit.p2 = pred.p2$fit
low.p2 = pred.p2$fit - 1.645*pred.p2$se.fit
high.p2 = pred.p2$fit + 1.645*pred.p2$se.fit
cis.p2 = cbind(fit.p2, low.p2, high.p2)

cis.p2

pred.p2.gg=as.data.frame(pred.p2)
cis.p2.gg=as.data.frame(cis.p2)

library(ggplot2)
ggplot(pred.p2.gg, aes(x=seq(-3,3,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p2.gg$low.p2, ymax=cis.p2.gg$high.p2), alpha=0.1, fill="Blue", color="Blue", linetype = 2) + scale_fill_brewer(palette="Blues") + xlab("Trust in Domestic Local Bureaucrcacy") + ylab("Trust in EU Bureaucracy") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=17,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5))



###########################
### PERCEIVED CORRUPTION IN THE EU BUREAUCRACY AND IN THE DOMESTIC CENTRAL BUREAUCRACY
### FIGURE 4 IN THE ARTICLE
###########################

### IDENTIFY PREDICTED VALUES

pred.p3 = predict(lm_corruption_03, type="response", se.fit=TRUE, newdata=nd3)
pred.p3

gch.table = cbind(pred.p3$fit, pred.p3$se.fit)
gch.table

### CREATE THE PLOT

fit.p3 = pred.p3
fit.p3 = pred.p3$fit
low.p3 = pred.p3$fit - 1.645*pred.p3$se.fit
high.p3 = pred.p3$fit + 1.645*pred.p3$se.fit
cis.p3 = cbind(fit.p3, low.p3, high.p3)

cis.p3

pred.p3.gg=as.data.frame(pred.p3)
cis.p3.gg=as.data.frame(cis.p3)

pred.p3.gg

library(ggplot2)
ggplot(pred.p3.gg, aes(x=seq(-3,3,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p3.gg$low.p3, ymax=cis.p3.gg$high.p3), alpha=0.1, fill="Blue", color="Blue", linetype = 2) + scale_fill_brewer(palette="Blues") + xlab("Perceived Corruption in Domestic Central Bureaucracy") + ylab("Perceived Corruption in EU Bureaucracy") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5))



###########################
### PERCEIVED CORRUPTION IN THE EU BUREAUCRACY AND IN THE DOMESTIC LOCAL BUREAUCRACY (WITHOUT CONTROLS)
### FIGURE 5 (PANEL A) IN THE ARTICLE
###########################

### IDENTIFY PREDICTED VALUES

pred.p4 = predict(lm_corruption_03, type="response", se.fit=TRUE, newdata=nd4)
pred.p4

gch.table = cbind(pred.p4$fit, pred.p4$se.fit)
gch.table

### CREATE THE PLOT

fit.p4 = pred.p4
fit.p4 = pred.p4$fit
low.p4 = pred.p4$fit - 1.645*pred.p4$se.fit
high.p4 = pred.p4$fit + 1.645*pred.p4$se.fit
cis.p4 = cbind(fit.p4, low.p4, high.p4)

cis.p4

pred.p4.gg=as.data.frame(pred.p4)
cis.p4.gg=as.data.frame(cis.p4)

library(ggplot2)
ggplot(pred.p4.gg, aes(x=seq(-3,3,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p4.gg$low.p4, ymax=cis.p4.gg$high.p4), alpha=0.1, fill="Blue", color="Blue", linetype = 2) + scale_fill_brewer(palette="Blues") + xlab("Perceived Corruption in Domestic Local Bureaucracy") + ylab("Perceived Corruption in EU Bureaucracy") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5))



###########################
### PERCEIVED CORRUPTION IN THE EU BUREAUCRACY AND IN THE DOMESTIC LOCAL BUREAUCRACY (WITH CONTROLS)
### FIGURE 5 (PANEL B) IN THE ARTICLE
###########################

### IDENTIFY PREDICTED VALUES

pred.p4 = predict(lm_corruption_06, type="response", se.fit=TRUE, newdata=nd4_controls)
pred.p4

gch.table = cbind(pred.p4$fit, pred.p4$se.fit)
gch.table

### CREATE THE PLOT

fit.p4 = pred.p4
fit.p4 = pred.p4$fit
low.p4 = pred.p4$fit - 1.645*pred.p4$se.fit
high.p4 = pred.p4$fit + 1.645*pred.p4$se.fit
cis.p4 = cbind(fit.p4, low.p4, high.p4)

cis.p4

pred.p4.gg=as.data.frame(pred.p4)
cis.p4.gg=as.data.frame(cis.p4)

library(ggplot2)
ggplot(pred.p4.gg, aes(x=seq(-3,3,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p4.gg$low.p4, ymax=cis.p4.gg$high.p4), alpha=0.1, fill="Blue", color="Blue", linetype = 2) + scale_fill_brewer(palette="Blues") + xlab("Perceived Corruption in Domestic Local Bureaucracy") + ylab("Perceived Corruption in EU Bureaucracy") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5))



###########################
### ADDITIONAL EMPIRICAL ANALYSES IN THE ONLINE APPENDIX
###########################

###########################
### REGRESSIONS WITH ROBUST STANDARD ERRORS
###########################

library(lmtest)
library(sandwich)



###########################
### TRUST IN THE EU BUREAUCRACY (ROBUST STANDARD ERRORS)
### TABLE A1 IN THE ONLINE APPENDIX
###########################

lm_trust_01_rob=coeftest(lm_trust_01, vcov = vcovHC(lm_trust_01))
lm_trust_01_rob

lm_trust_02_rob=coeftest(lm_trust_02, vcov = vcovHC(lm_trust_02))
lm_trust_02_rob

lm_trust_03_rob=coeftest(lm_trust_03, vcov = vcovHC(lm_trust_03))
lm_trust_03_rob

lm_trust_04_rob=coeftest(lm_trust_04, vcov = vcovHC(lm_trust_04))
lm_trust_04_rob

lm_trust_05_rob=coeftest(lm_trust_05, vcov = vcovHC(lm_trust_05))
lm_trust_05_rob

lm_trust_06_rob=coeftest(lm_trust_06, vcov = vcovHC(lm_trust_06))
lm_trust_06_rob

lm_trust_full_rob=coeftest(lm_trust_full, vcov = vcovHC(lm_trust_full))
lm_trust_full_rob



###########################
### PERCEPTIONS OF CORRUPTION IN THE EU BUREAUCRACY (ROBUST STANDARD ERRORS)
### TABLE A2 IN THE ONLINE APPENDIX
###########################

lm_corruption_01_rob=coeftest(lm_corruption_01, vcov = vcovHC(lm_corruption_01))
lm_corruption_01_rob

lm_corruption_02_rob=coeftest(lm_corruption_02, vcov = vcovHC(lm_corruption_02))
lm_corruption_02_rob

lm_corruption_03_rob=coeftest(lm_corruption_03, vcov = vcovHC(lm_corruption_03))
lm_corruption_03_rob

lm_corruption_04_rob=coeftest(lm_corruption_04, vcov = vcovHC(lm_corruption_04))
lm_corruption_04_rob

lm_corruption_05_rob=coeftest(lm_corruption_05, vcov = vcovHC(lm_corruption_05))
lm_corruption_05_rob

lm_corruption_06_rob=coeftest(lm_corruption_06, vcov = vcovHC(lm_corruption_06))
lm_corruption_06_rob

lm_corruption_full_rob=coeftest(lm_corruption_full, vcov = vcovHC(lm_corruption_full))
lm_corruption_full_rob



###########################
### ADDITIONAL REGRESSIONS ON GENERAL FORMS OF TRUST IN PUBLIC INSTITUTIONS
### TABLE A3 IN THE ONLINE APPENDIX
###########################

lm_trust_extra_01=lm(trust_EUpa ~ trust_police + trust_courts, data=EU_publicadmin_data)
summary(lm_trust_extra_01)

lm_trust_extra_02=lm(trust_EUpa ~ trust_police + trust_courts + trust_centpa + trust_locpa, data=EU_publicadmin_data)
summary(lm_trust_extra_02)

lm_corruption_extra_01=lm(corr_EUpa ~ trust_police + trust_courts, data=EU_publicadmin_data)
summary(lm_corruption_extra_01)

lm_corruption_extra_02=lm(corr_EUpa ~ trust_police + trust_courts + corr_centpa + corruption, data=EU_publicadmin_data)
summary(lm_corruption_extra_02)



###########################
### TRUST: EXPLORATION OF HETEROGENEOUS EFFECTS
### TABLE A4 IN THE ONLINE APPENDIX
###########################

lm_trust_01_het=lm(trust_EUpa ~ trust_centpa + trust_locpa + int_trust_centpa_educ + int_trust_locpa_educ + direc_econ + EU_econeff + trust_police + trust_courts + AU + factor(type) + age + work_pa + educ + income + female + capital, data=EU_publicadmin_data)
summary(lm_trust_01_het)

lm_trust_02_het=lm(trust_EUpa ~ trust_centpa + trust_locpa + int_trust_centpa_income + int_trust_locpa_income + direc_econ + EU_econeff + trust_police + trust_courts + AU + factor(type) + age + work_pa + educ + income + female + capital, data=EU_publicadmin_data)
summary(lm_trust_02_het)



###########################
### CORRUPTION: EXPLORATION OF HETEROGENEOUS EFFECTS
### TABLE A5 IN THE APPENDIX
###########################

lm_corruption_01_het=lm(corr_EUpa ~ corr_centpa + corruption + int_corr_centpa_educ + int_corruption_educ + direc_econ + EU_econeff + trust_police + trust_courts + AU + factor(type) + age + work_pa + educ + income + female + capital, data=EU_publicadmin_data)
summary(lm_corruption_01_het)

lm_corruption_02_het=lm(corr_EUpa ~ corr_centpa + corruption + int_corr_centpa_income + int_corruption_income + direc_econ + EU_econeff + trust_police + trust_courts + AU + factor(type) + age + work_pa + educ + income + female + capital, data=EU_publicadmin_data)
summary(lm_corruption_02_het)


